package loveappservice.util;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

public class QiniuUtil {

    // 配置类
    Configuration cfg = new Configuration(Region.region2());
    UploadManager uploadManager = new UploadManager(cfg);
    // 上传凭证
    String accessKey = "7QfFZLXmZkupYXp_hrEf_NEA6XYrkI6stdfg3Ent";
    String secretKey = "rBvRVf3vUfWg9wchPzRVWKXJMNke0ftMzdGJJUqi";
    // 空间名称
    String bucket = "testtuchuang";

/*    public String upData(){
        // 配置类
        Configuration cfg = new Configuration(Region.region2());
        UploadManager uploadManager = new UploadManager(cfg);
        // 上传凭证
        String accessKey = "7QfFZLXmZkupYXp_hrEf_NEA6XYrkI6stdfg3Ent";
        String secretKey = "rBvRVf3vUfWg9wchPzRVWKXJMNke0ftMzdGJJUqi";
        // 空间名称
        String bucket = "testtuchuang";
        // 图片地址
        String localFilePath = "D:\\Desktop\\UI界面\\test.jpg";
        // 图片上传后名称
        String key = "love-app/test.jpg";
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);

        try {
            Response response = uploadManager.put(localFilePath, key, upToken);
            // 解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println("嗨客网(www.haicoder.net)\n");
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                ex2.printStackTrace();
            }
        }
        return "ok";
    }*/

    /*文件输入流上传到七牛云*/
    public String uploadImg(String images_name , String fileName ,MultipartFile file) throws IOException {


        /*获取图片输入流*/
        InputStream inputStream = file.getInputStream();

        // 图片上传后名称
        String key = "love-app/images/"+images_name+"/"+fileName;
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);

        try {
            Response response = uploadManager.put(inputStream,key,upToken,null, null);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);



        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
        return "ok";
    }
    public boolean delectFile(String key){
        System.out.println("删除了七牛云的照片，key为："+key);

        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region0());

        Auth auth = Auth.create(accessKey, secretKey);

        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.delete(bucket, key);
            return true;
        } catch (QiniuException ex) {
            //如果遇到异常，说明删除失败
            System.err.println(ex.code());
            System.err.println(ex.response.toString());
            return false;
        }

    }

}
